﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace c1
{

    //链表节点
    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }
        public Node(T t)
        {
            Next = null;
            Data = t; }
    }

    //链表类
    public class GenericList<T> {
        private Node<T> head;
        private Node<T> tail;
        public GenericList() {
            tail = head = null; }
        public Node<T> Head { get => head; }

        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }

        public void ForEach(Action<T> action) {
            Node<T> cu = head;
            while (cu.Next != null)
            {
                action(cu.Data);
                cu = cu.Next;
            }
        }

    }
    internal class Program
    {
        static void Main(string[] args)
        {

            GenericList<int> b = new GenericList<int>();
            string c = "";
            Console.WriteLine("Enter Array's elements, end with \";\"");
            while (c != ";")
            {
                c = Console.ReadLine();
                try
                {
                    if (c == ";")
                    {
                        break;
                    }
                    b.Add(int.Parse(c));
                }
                catch (Exception)
                {

                    Console.WriteLine("Invalid Input! Cannot put this into an integer array!");
                }
            }
            /*打印*/
            b.ForEach(n =>Console.Write(n+","));
            Console.Write("\n");

            /*求max*/
            int max=0;
            b.ForEach(n => { if (n > max) { max = n; } });
            Console.Write("max is "+max);
            Console.Write("\n");

            /*求min*/
            int min = int.MaxValue;
            b.ForEach(n => { if (n < min) { min = n; } });
            Console.Write("min is "+min);
            Console.Write("\n");

            /*求sum*/
            int sum = 0;
            b.ForEach(n => sum += n);
            Console.Write("sum is "+sum);
            Console.Write("\n");

            Console.ReadKey();
        }
    }
}
